package com.s63.appinfo.service.impl;

import com.s63.appinfo.mapper.AppInfoMapper;
import com.s63.appinfo.mapper.AppVersionMapper;
import com.s63.appinfo.pojo.AppInfo;
import com.s63.appinfo.pojo.AppVersion;
import com.s63.appinfo.service.AppVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class AppVersionServiceImpl implements AppVersionService {
    @Autowired
    private AppVersionMapper appVersionMapper;
    @Autowired
    private AppInfoMapper appInfoMapper;

    @Override
    public List<AppVersion> getByAppId(Long appId) {
        return appVersionMapper.selectByAppId(appId);
    }

    /**
     * 1、保存version
     * 2、更新appinfo中的最新版本号
     * @param appVersion
     * @return
     */
    @Transactional
    @Override
    public boolean save(AppVersion appVersion) {
        // 1
        appVersion.setCreationDate(new Date());
        appVersionMapper.insert(appVersion);
        // 2 更新appinfo中的最新版本号
        AppInfo appInfo = appInfoMapper.selectByPrimaryKey(appVersion.getAppId());
        appInfo.setVersionId(appVersion.getId());
        appInfoMapper.updateByPrimaryKeySelective(appInfo);
        return true;
    }
}
